import 'dart:io';

/**
 * dart 单线程语言。
 * 异步
 *
 *
 * I/flutter (23711): begin!
    I/flutter (23711): do completed!
    I/flutter (23711): end!
 */

/**
 * 单线程
 */
void doTask() {
  sleep(Duration(seconds: 2)); //单线程, 改为异步 async await
  print('do completed!');
}

/**
 * 异步执行，将事件放入消息队列
 */
void doTask2() async {
  // await sleep(Duration(seconds: 2)); //单线程, 改为异步 async await
  // print('do completed!');
}

String getSome() {
  print('get something');
  return 'zhangshan';
}

Future<String> method5() async {
  return '5';
}

Future<String> method6() async {
  return '6';
}

Future<String> method7() async {
  return '7';
}

void testE() {
  Future.wait([method5(), method6(), method7()]).then((List response) {
    print(response);
  }).catchError((e) {
    print(e.toString());
  });
}

Future<int> method8() async {
  return 8;
}

Future<int> method9(int p) async {
  return p + 9;
}

Future<int> method10(int p) async {
  return p + 10;
}

void testG() {
  method8().then((value8) {
    print('value8=$value8');
    return method9(value8);
  }).then((value9) {
    print('value9=$value9');
    return method10(value9);
  }).then((value10) {
    print('value10=$value10');
  });
}

void main() {
  // print('begin!');
  // doTask();
  // print('end!');

  /**
   * 拿到异步返回的值
   */
  Future<String> name = new Future(getSome);
  name.then((value) {
    print(value);
  });

  testE();
  testG();
}
